#!/usr/bin/env python

import logging

import wsgiref.handlers

from google.appengine.ext import webapp

from django.utils import simplejson

from entities import document

class UpdateHandler(webapp.RequestHandler):
    def get(self):
        document_id = self.request.get("id")
        client_id = self.request.get("client_id")
        
        if(document_id != ''):
            doc = document.Document.get_by_id(int(document_id))
            if ( doc ):
                self.documentJSONResponse(doc)
            else:
                errorResponse("Can't found document with id "+document_id)
                  
        else:
            self.errorResponse("You must provide id for this request")
        
    def errorResponse(self, message):
        errorData = {"error":message}
        self.response.out.write(simplejson.dumps(errorData))
        
    def documentJSONResponse(self, doc):
        logging.debug("isUpdated : "+str(doc.isUpdated))
        
        docData = {
            "id":doc.key().id(), 
            "documentName":doc.documentName, 
            "documentType":doc.documentType,
            "documentContent":doc.documentContent
            }
        
        doc.isUpdated=False
        doc.put()
            
        self.response.out.write(simplejson.dumps(docData))
        
